package com.zmx.core.server;

import com.zmx.core.dto.HeartInfo;
import lombok.Data;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 客户端实例信息
 * @ClassName: ClientExampleInfo
 * @Author zhaomxr
 * @Date 2021-12-16 14:40
 */
@Data
@ToString
public class ClientInstanceInfo implements Serializable {

    /**
     * 存活节点 是否在线（true/false）
     */
    private static Map<String, Boolean> onLineInstance;

    /**
     * 存活节点 内存/cpu等数据信息
     */
    private static Map<String, HeartInfo> infoInstance;

    /**
     * 最后一次更新时间
     */
    private static long lastUpdate = System.currentTimeMillis();


    public static void setOnLineInstance(String node, boolean onLine) {
        if (onLineInstance != null) {
            synchronized (onLineInstance) {
                if (onLineInstance != null) {
                    onLineInstance.put(node, onLine);
                } else {
                    onLineInstance = new HashMap<String, Boolean>(16);
                    setOnLineInstance(node, onLine);
                }
            }
        } else {
            onLineInstance = new HashMap<String, Boolean>(16);
            setOnLineInstance(node, onLine);
        }
        lastUpdate = System.currentTimeMillis();
    }

    public static void setInfoInstance(HeartInfo heartInfo) {
        if (infoInstance != null) {
            synchronized (infoInstance) {
                if (infoInstance != null) {
                    infoInstance.put(heartInfo.getIp() + ":" + heartInfo.getPort(), heartInfo);
                } else {
                    infoInstance = new HashMap<String, HeartInfo>(16);
                    setInfoInstance(heartInfo);
                }
            }
        } else {
            infoInstance = new HashMap<String, HeartInfo>(16);
            setInfoInstance(heartInfo);
        }
    }

    public static Map<String, Boolean> getOnLineInstance() {
        return onLineInstance;
    }

    public static Boolean getOnLineInstance(String node) {
        if (onLineInstance == null || StringUtils.isEmpty(node)) {
            return false;
        }
        return onLineInstance.get(node);
    }

    public static Map<String, HeartInfo> getInfoInstance() {
        return infoInstance;
    }

    public static HeartInfo getInfoInstance(String node) {
        if (infoInstance == null || StringUtils.isEmpty(node)) {
            return null;
        }
        return infoInstance.get(node);
    }

    /**
     * 删除节点
     * @param node
     */
    public static void removeNode(String node) {
        if (onLineInstance.get(node) != null) {
            onLineInstance.remove(node);
        }
        if (infoInstance.get(node) != null) {
            infoInstance.remove(node);
        }
        //此处必须更新最后一次刷新时间，不然 加权节点数据 不刷新
        lastUpdate = System.currentTimeMillis();
    }

    public static long getLastUpdate() {
        return lastUpdate;
    }

}
